#include "TcpTask.h"

TcpTask::TcpTask(QObject *parent)
    : QObject(parent),
    tcpSocket(new QTcpSocket(this))
{
    connect(tcpSocket, &QTcpSocket::connected, this, &TcpTask::onConnected);
    connect(tcpSocket, &QTcpSocket::readyRead, this, &TcpTask::onReadyRead);
    connect(tcpSocket, &QTcpSocket::disconnected, this, &TcpTask::onDisconnected);
    connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::errorOccurred), this, &TcpTask::onErrorOccurred);

    this -> connectToServer("127.0.0.1",8888);
}

TcpTask::~TcpTask()
{
    qDebug() << "~NetManager";
    tcpSocket->disconnectFromHost();
    tcpSocket->waitForDisconnected();
}

bool TcpTask::connectToServer(const QString &host, quint16 port)
{
    tcpSocket->connectToHost(host, port);
    return tcpSocket->waitForConnected();
}

void TcpTask::sendCommand(const QByteArray &data)
{
    if (tcpSocket->state() == QTcpSocket::ConnectedState) {
        tcpSocket->write(data);
        tcpSocket->flush();
        tcpSocket->waitForReadyRead(3000);

    } else {
        qDebug() << "Not connected to server.";
    }
}

void TcpTask::onConnected()
{
    qDebug() << "Connected to server.";
}

void TcpTask::onDisconnected()
{
    qDebug() << "Disconnected from server.";
    emit free();
}

void TcpTask::onErrorOccurred(QAbstractSocket::SocketError socketError)
{
    qDebug() << "Error occurred:" << socketError;
}


void TcpTask::onReadyRead()
{
    QByteArray data = tcpSocket->readAll();
    QDataStream in(data);
    Header header;
    in >> header;

    // TODO

    switch (header) {
    case RETURN_CHAT_HISTORY_WITH_USER_ID_OK:
    {
        int userId1, userId2;
        in >> userId1 >> userId2;
        quint32 listSize;
        in >>listSize;

        QList<ChatHistory> chatList;
        for (quint32 i = 0 ; i < listSize ; i ++)
        {
            ChatHistory temData;
            in >> temData;
            chatList.append(temData);
        }

        emit ChatHistoryReady(chatList);
        break;
    }
    case RETURN_CHAT_HISTORY_WITH_USER_ID_ERROR:
    {
        emit chatHistoryError();
        break;
    }
    case RETURN_FRIEND_LIST_WITH_USER_ID_OK:
    {
        int userId4;
        in >> userId4;
        quint32 listSize2;
        in >> listSize2;

        QList<Friend> friendList;
        for(quint32 i = 0 ; i < listSize2 ; i ++)
        {
            Friend temFriend;
            in >> temFriend;
            friendList.append(temFriend);
        }

        emit FriendListReady(friendList);
        break;
    }
    case RETURN_FRIEND_LIST_WITH_USER_ID_ERROR:
    {
        emit FriendListError();
        break;
    }
    case RETURN_INFORMATION_WITH_USER_ID_OK:
    {
        int userId3;
        in >> userId3;
        User temUser;
        in >> temUser;

        emit UserInformationReady(temUser);
        break;
    }
    case RETURN_INFORMATION_WITH_USER_ID_ERROR:
    {
        emit UserInformationError();
        break;
    }
    case INSERT_CHAT_HISTORY_OK:
    {
        emit InsertChatHistoryOk();
        break;
    }
    case INSERT_CHAT_HISTORY_ERROR:
    {
        emit InsertChatHistoryError();
        break;
    }
    case UPDATE_USER_INFORMATION_OK:
    {
        emit UpdateChatInformationOk();
        break;
    }
    case UPDATE_USER_INFORMATION_ERROR:
    {
        emit UpdateChatInformationError();
        break;
    }
    case INSERT_USER_OK:
    {
        emit InsertUserOk();
        break;
    }
    case INSERT_USER_ERROR:
    {
        emit InsertUserError();
        break;
    }
    case INSERT_FRIEND_RELATION_OK:
    {
        emit insertFriendRelationOk();
        break;
    }
    case INSERT_FRIEND_RELATION_ERROR:
    {
        emit insertFriendRelationError();
        break;
    }
    case DELETE_FRIEND_RELATION_OK:
    {
        emit DeleteFriendRelationOk();
        break;
    }
    case DELETE_FRIEND_RELATION_ERROR:
    {
        emit DeleteFriendRelationError();
        break;
    }
    default:
        break;
    }

    // 断开链接
    tcpSocket->disconnectFromHost();
}

